
import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
/**
 *
 * @author user
 */
public class Acceuil extends javax.swing.JFrame {

    private String user;
    private String password;
    private String function;

    public Acceuil() {
        initComponents();
        
        Font police = new Font("Arial", Font.BOLD, 14);
        Nom_Utilisateur.setFont(police);
        // Nom_Utilisateur.setPreferredSize(new Dimension(150, 30));
        //Nom_Utilisateur.setForeground(Color.BLUE);
        

        this.setTitle(" Net Trading ");
        //Définit une taille pour celle-ci ; ici, 400 px de large et 500 px de haut
        //this.setSize(400, 500);
        //Nous allons maintenant dire à notre objet de se positionner au centre
        this.setLocationRelativeTo(null);
        //Terminer le processus lorsqu'on clique sur "Fermer"
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panheader = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        pan = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        Mot_de_Passe = new javax.swing.JPasswordField();
        Nom_Utilisateur = new javax.swing.JTextField();
        Connecter = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        panheader.setBackground(new java.awt.Color(0, 153, 153));

        jLabel6.setFont(new java.awt.Font("Vani", 1, 14)); // NOI18N
        jLabel6.setText("NET TRADING");

        javax.swing.GroupLayout panheaderLayout = new javax.swing.GroupLayout(panheader);
        panheader.setLayout(panheaderLayout);
        panheaderLayout.setHorizontalGroup(
            panheaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panheaderLayout.createSequentialGroup()
                .addGap(249, 249, 249)
                .addComponent(jLabel6)
                .addContainerGap(292, Short.MAX_VALUE))
        );
        panheaderLayout.setVerticalGroup(
            panheaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, 49, Short.MAX_VALUE)
        );

        jLabel1.setText("Nom Utisateur");

        jLabel2.setText("Mot de passe");

        Mot_de_Passe.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Mot_de_PasseActionPerformed(evt);
            }
        });

        Nom_Utilisateur.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Nom_UtilisateurActionPerformed(evt);
            }
        });

        Connecter.setBackground(new java.awt.Color(255, 255, 255));
        Connecter.setText("Connecter");
        Connecter.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ConnecterActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panLayout = new javax.swing.GroupLayout(pan);
        pan.setLayout(panLayout);
        panLayout.setHorizontalGroup(
            panLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panLayout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(panLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(panLayout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(Nom_Utilisateur, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(panLayout.createSequentialGroup()
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(panLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(Mot_de_Passe)
                            .addComponent(Connecter, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(35, Short.MAX_VALUE))
        );
        panLayout.setVerticalGroup(
            panLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Nom_Utilisateur, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(panLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Mot_de_Passe, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addComponent(Connecter)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panheader, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(pan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(panheader, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(172, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void ConnecterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ConnecterActionPerformed
        try {
            user = Nom_Utilisateur.getText().toUpperCase();
            while (user.indexOf(" ") == 0) {
                user = user.substring(0, user.length());
            }
            if (!user.replace(" ", "").matches(DbManip.regexLettreNumUnderscore)) {
                JOptionPane.showMessageDialog(null, "Vérifiez le nom", "Attention", JOptionPane.ERROR_MESSAGE);
                Nom_Utilisateur.setText("");
                return;
            }
            password = Mot_de_Passe.getText();
            if (!password.matches(DbManip.regexLettreNumUnderscore)) {
                JOptionPane.showMessageDialog(null, "Vérifiez le mot de passe", "Attention", JOptionPane.ERROR_MESSAGE);
                Mot_de_Passe.setText("");
                return;
            }
            DbManip.requete = "SELECT * FROM UTILISATEUR WHERE NOM_UTILSATEUR='" + user + "' and MOT_DE_PASSE ='" + DbManip.hash.encode(password) + "'";
            System.out.println("req" + Mot_de_Passe + "   " + Nom_Utilisateur);
            DbManip.executer(DbManip.requete);
            boolean exist = (DbManip.rs.next()) && (DbManip.rs != null);
            if (exist) {
                dispose();
                DbManip.requete = "SELECT TYPE  FROM UTILISATEUR WHERE NOM_UTILSATEUR='" + user + "' and MOT_DE_PASSE ='" + DbManip.hash.encode(password) + "'";
                DbManip.executer(DbManip.requete);
                String typeUser = "";
                while (DbManip.rs.next()) {
                    typeUser = DbManip.rs.getString(1);
                }
                System.out.println("type=" + typeUser);

                if ("TRADER".equals(typeUser)) {
                    String ch1 = user.substring(0,user.indexOf(" "));// prenom
                    String ch2 = user.substring(user.indexOf(" ") + 1); // nom
                    DbManip.requete = "SELECT MATRICULE  FROM trader WHERE NOM_TRADER='" + ch2 + "' and PRENOM_TRADER ='" + ch1 + "'";
                    DbManip.executer(DbManip.requete);
                    
                    // Changement de  traderId
                    while (DbManip.rs.next()) {
                        DbManip.traderId = DbManip.rs.getString(1);
                    }
                    
                    // Changement du nom utilisateur courant
                    DbManip.utilisateur= user; 
                    // Ouverture de la page d'acceuil du Trader                    
                    
                    AcceuilTrader tradAcc= new AcceuilTrader();
                    tradAcc.setVisible(true);
                    
                    
                    
                }
                else{
                    // Ouvrir la page d'acceuil d'administration 
                    
                    // changement de utilisateur
                    DbManip.utilisateur= user; 
                    
                    Administration adm = new Administration();
                    adm.setVisible(true);
                    
                    
                }
                
            }
            else {
                JOptionPane.showMessageDialog(null, "Ce nom d'utilisateur n'est pas utilisé !!", "Attention", JOptionPane.ERROR_MESSAGE);
                Nom_Utilisateur.setText("");
                Mot_de_Passe.setText("");


            }
        }
        catch (SQLException ex) {
            Logger.getLogger(Acceuil.class.getName()).log(Level.SEVERE, null, ex);
        }






    }//GEN-LAST:event_ConnecterActionPerformed

    private void Nom_UtilisateurActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Nom_UtilisateurActionPerformed

        user = evt.getActionCommand();
        System.out.println("user is " + user);
        // TODO add your handling code here:
    }//GEN-LAST:event_Nom_UtilisateurActionPerformed

    private void Mot_de_PasseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Mot_de_PasseActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_Mot_de_PasseActionPerformed
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Connecter;
    private javax.swing.JPasswordField Mot_de_Passe;
    private javax.swing.JTextField Nom_Utilisateur;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel pan;
    private javax.swing.JPanel panheader;
    // End of variables declaration//GEN-END:variables
}
